<template>
    <el-form ref="form" :model="form" label-width="auto" size="mini" label-position="left">  
        <el-form-item>
            <div class="bigsize">
                <h5>1xx代表女生宿舍，2xx代表男生宿舍</h5>
                栋号：<el-input ref="building" v-model="form.building"></el-input> -
                共 <el-input ref="floor" v-model="form.floor"></el-input> 楼-
                每层 <el-input ref="room" v-model="form.room"></el-input> 间宿舍
            </div>
        </el-form-item>
        <el-form-item style="display:block" size="large">
            <el-button type="primary" @click="onSubmit(form)">立即创建</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    name:'Adddormitory',
    data() {
        return {
            form:{
                building:'0',
                floor:'0',
                room:'0'
            },
            check:[]
        }
    },
    methods:{
        load(){
            let _this = this
            axios.get('http://localhost:8081/admin/check').then(function(resp){
                _this.check = resp.data
            })
        },
        onSubmit(form){
            let _this = this
            let k = 0
            if(isNaN(parseInt(form.building)) || isNaN(parseInt(form.floor)) || isNaN(parseInt(form.room))){
                alert("请不要输入字母");
                return;
            }
            if(parseInt(form.building)<100 || parseInt(form.building)>=300){
                alert("栋号超出范围");
                this.$refs.building.focus()
                return;
            }
            if(parseInt(form.floor)<=0){
                alert("不能有负数或0楼层");
                this.$refs.floor.focus()
                return;
            }
            if(parseInt(form.room)<=0){
                alert("不能有负数或0间房间");
                this.$refs.room.focus()
                return;
            }
            this.check.forEach(element => {
                if(this.form.building == element){
                    k=1;
                    return;
                }
            });
            if(k==1){
                alert('该栋号已被使用，请重新输入')
                this.$refs.building.focus()
                return
            }
            axios.get('http://localhost:8081/admin/adddormitory',{params:_this.form}).then(function(resp){
                if(resp.data){
                    alert('添加成功')
                    _this.load();
                }
            })
        }
    },
    mounted(){
        this.load();
    }
}
</script>

<style scoped>
    .el-form{
        border-radius: 5px;
        width: 500px;
    }
    .el-form-item{
        text-align: center;
    }
    .el-input{
        display: inline-block;
        width: 70px;
        font-size: 14px;
    }
    .bigsize{
        font-size: 20px;
    }
</style>